package com.example.baselib.utils;

import com.example.baselib.ResourceTable;
import com.example.baselib.utils.log.LogUtils;
import ohos.agp.components.*;
import ohos.agp.window.dialog.ToastDialog;
import ohos.agp.window.service.Window;
import ohos.app.Context;

/**
 * Description:  TODO
 * Author: admin
 * CreateDate: 2021/4/2 10:35
 */
public class ToastUtils {

    private static ToastDialog toastDialog;

    public static void show(Context context, String text) {
        if (context == null) {
            LogUtils.e("ToastDialog show() context == null");
            return;
        }
        createToast(context);
        Text tt = UIUtils.findComponentFromId(toastDialog.getComponent(), ResourceTable.Id_toast_text);
        tt.setText(text);
        toastDialog.setTransparent(true);
        toastDialog.show();
        Window window = toastDialog.getWindow();
        window.setTransparent(true);
    }

    private static void createToast(Context context) {
        if (toastDialog == null) {
            synchronized (ToastUtils.class) {
                if (toastDialog == null) {
                    toastDialog = new ToastDialog(context);
                }
            }
        }
        toastDialog.setDuration(2500);
        toastDialog.setComponent(LayoutScatter.getInstance(context).parse(ResourceTable.Layout_toast_layout, null, false));
    }




}
